import React, { Component } from 'react'
import {goodsRequest} from "../request/cate_request"
import qs from "qs"
import Header from '../components/DefaultHeader'
import {cartAdd} from '../request/cart_request'
export default class GoodsList extends Component {
    state = {
        type_name:'',
        goods_list:[]
    }
    addCar(goodsid,type=2){
        //type确定是万人团，是2
        //num确定为1
        cartAdd(goodsid,type,1).then(res=>{
           if(res.code == 200){
            this.props.restCarList()
           }
        })

    }
    UNSAFE_componentWillMount(){
        const {type_id,type_name} = qs.parse(this.props.location.search.substring(1))
        this.setState({type_name})
        goodsRequest(type_id).then(res=>{
            this.setState({goods_list:res.list})
        })
    }

    render() {
        const {type_name,goods_list} = this.state
        return (
            <div className="goods-list-container">
            <div className="page-title">{type_name}</div>
            <Header title='商品列表' right_text='登录' right_method={()=>{this.props.history.push('/login')}} />
            <div className="goodslist">
                {
                    goods_list.map(item=>(
                        <div className="item" key={item.id}> 
                    <img onClick={()=>this.props.history.push('/goods_detail/?id='+item.id)} src={this.$static_host+item.img} alt="" />
                    <div>
                        <h5 onClick={()=>this.props.history.push('/goods_detail/?id='+item.id)}>{item.goodsname}</h5>
                        <p className="price">&yen;{item.price}</p>
                        <p className="market-price">&yen;{item.market_price}</p>
                        <button className="btn-cart" onClick={()=>this.addCar(item.id,1)}>加入购物车</button>
                    </div>
                </div>
                    ))
                }
                
            </div>
        </div>
        )
    }
}
